#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QFileDialog>
#include <QLabel>
#include <QScrollArea>
#include <QGridLayout>
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 创建文件菜单
    QMenu *fileMenu = menuBar()->addMenu(tr("文件"));

     QMenu *testMenu = menuBar()->addMenu(tr("测试"));

    // 添加"选择文件"动作
    QAction *selectFileAction = new QAction(tr("选择文件"), this);
    fileMenu->addAction(selectFileAction);
    connect(selectFileAction, &QAction::triggered, this, &MainWindow::selectFile);

    // 添加"选择文件夹"动作
    QAction *selectFolderAction = new QAction(tr("选择文件夹"), this);
    fileMenu->addAction(selectFolderAction);
    connect(selectFolderAction, &QAction::triggered, this, &MainWindow::selectFolder);

    createFeatureMenu();
    loadFeatureSettings();

    // 创建中央部件和滚动区域
    QWidget *centralWidget = new QWidget(this);
    QScrollArea *scrollArea = new QScrollArea(centralWidget);
    scrollArea->setWidgetResizable(true);
    setCentralWidget(centralWidget);

    QVBoxLayout *layout = new QVBoxLayout(centralWidget);
    layout->addWidget(scrollArea);

    contentWidget = new QWidget(scrollArea);
    scrollArea->setWidget(contentWidget);

    gridLayout = new QGridLayout(contentWidget);

    resize(800, 600);
}

MainWindow::~MainWindow()
{
    saveFeatureSettings();
    delete ui;
}


void MainWindow::createFeatureMenu()
{
    QMenu *featureMenu = menuBar()->addMenu(tr("增强"));
    QMenu *featureConfigMenu = featureMenu->addMenu(tr("增强选项"));

    QStringList features = {"明暗度", "旋转", "剪切", "噪声添加", "翻转"};
    for (const QString &feature : features)
    {
        CheckableMenuAction *action = new CheckableMenuAction(feature, this);
        featureConfigMenu->addAction(action);

        featureActions[feature] = action;
        connect(action, &CheckableMenuAction::toggled, this, &MainWindow::onFeatureActionToggled);
    }
}
void MainWindow::onFeatureActionToggled(bool checked)
{
    CheckableMenuAction *action = qobject_cast<CheckableMenuAction*>(sender());
    if (action)
    {
        QString featureName = action->text();
        //qInfo() << "Feature" << featureName << "is now" << (checked ? "enabled" : "disabled");
        //std::cout << "测试代码"<<std::endl;
        qDebug() << "测试代码"<< endl;
        // 这里可以添加根据功能开关状态执行相应操作的代码
    }
}

void MainWindow::saveFeatureSettings()
{
    QSettings settings("YourCompany", "YourApp");
    settings.beginGroup("FeatureSettings");
    for (auto it = featureActions.begin(); it != featureActions.end(); ++it)
    {
        settings.setValue(it.key(), it.value()->isChecked());
    }
    settings.endGroup();
}

void MainWindow::loadFeatureSettings()
{
    QSettings settings("YourCompany", "YourApp");
    settings.beginGroup("FeatureSettings");
    for (auto it = featureActions.begin(); it != featureActions.end(); ++it)
    {
        bool checked = settings.value(it.key(), false).toBool();
        it.value()->setChecked(checked);
    }
    settings.endGroup();
}


void MainWindow::selectFile()
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("Images (*.png *.jpg *.jpeg *.bmp)"));
    if (!filePath.isEmpty())
    {
        clearImages();
        displayImage(filePath);
    }
}

void MainWindow::selectFolder()
{
    QString folderPath = QFileDialog::getExistingDirectory(this, tr("选择文件夹"));
    if (!folderPath.isEmpty())
    {
        clearImages();
        displayImagesFromFolder(folderPath);
    }
}

void MainWindow::clearImages()
{
    while (QLayoutItem *item = gridLayout->takeAt(0))
    {
        delete item->widget();
        delete item;
    }
}

void MainWindow::displayImage(const QString &filePath)
{
    QLabel *imageLabel = new QLabel(contentWidget);
    QPixmap pixmap(filePath);
    imageLabel->setPixmap(pixmap.scaled(400, 400, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    gridLayout->addWidget(imageLabel, 0, 0, Qt::AlignCenter);
}

void MainWindow::displayImagesFromFolder(const QString &folderPath)
{
    QDir dir(folderPath);
    QStringList images = dir.entryList(QStringList() << "*.png" << "*.jpg" << "*.jpeg" << "*.bmp", QDir::Files);

    int row = 0, col = 0;
    for (const QString &fileName : images)
    {
        QLabel *imageLabel = new QLabel(contentWidget);
        QPixmap pixmap(dir.filePath(fileName));
        imageLabel->setPixmap(pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        gridLayout->addWidget(imageLabel, row, col);

        if (++col == 4)
        {
            col = 0;
            ++row;
        }
    }
}
